//  
//  Copyright (C) 2009 alex
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using System.ComponentModel;
using Gtk;

namespace twitalotgtk
{
	
	
	public partial class PreferencesWindow : Gtk.Window
	{			
		public PreferencesWindow() : 
				base(Gtk.WindowType.Toplevel)
		{
			this.Build();
			
			MainClass.Settings.PropertyChanged += HandlePropertyChanged;
			
			ApplySettings();
		}

		void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			ApplySettings();
		}		
		
		private void ApplySettings()
		{
			txtUsername.Text = MainClass.Settings.Username;
			txtPassword.Sensitive = chkStorePassword.Active = MainClass.Settings.StorePassword;
			txtPassword.Text = MainClass.Settings.Password;
			
			//Colours
			clrWindow.Color = MainClass.Settings.WindowColour;			
			clrRow.Color = MainClass.Settings.RowColour;
			clrAltRow.Color = MainClass.Settings.AltRowColour;
			clrText.Color = MainClass.Settings.RowTextColour;
			clrAltText.Color = MainClass.Settings.AltRowTextColour;
			clrHyperlink.Color = MainClass.Settings.HyperLinkColour;
			clrAltHyperlink.Color = MainClass.Settings.AltHyperLinkColour;
			clrInputText.Color = MainClass.Settings.InputTextColour;
			clrInputArea.Color = MainClass.Settings.InputBackgroundColour;
			rbRelative.Active = MainClass.Settings.RelativeTime;
			
			eventbox1.ModifyBg(StateType.Normal, MainClass.Settings.WindowColour);
		}
		
		protected virtual void chkStorePassword_Toggled (object sender, System.EventArgs e)
		{
			MainClass.Settings.StorePassword = txtPassword.Sensitive = chkStorePassword.Active;
			if(!MainClass.Settings.StorePassword)
				txtPassword.Text = string.Empty;
		}

		protected virtual void OnTxtUsernameEditingDone (object sender, System.EventArgs e)
		{
			MainClass.Settings.Username = txtUsername.Text;
		}

		protected virtual void OnBtnCloseClicked (object sender, System.EventArgs e)
		{
			this.Destroy();
		}

		protected virtual void OnClrWindowColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.WindowColour = clrWindow.Color;
		}

		protected virtual void OnClrInputAreaColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.InputBackgroundColour = clrInputArea.Color;
		}

		protected virtual void OnClrInputTextColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.InputTextColour = clrInputText.Color;
		}

		protected virtual void OnClrAltTextColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.AltRowTextColour = clrAltText.Color;
		}

		protected virtual void OnClrAltRowColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.AltRowColour = clrAltRow.Color;
		}

		protected virtual void OnClrTextColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.RowTextColour = clrText.Color;
		}

		protected virtual void OnClrRowColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.RowColour = clrRow.Color;
		}

		protected virtual void OnClrHyperlinkColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.HyperLinkColour = clrHyperlink.Color;
		}

		protected virtual void OnClrAltHyperlinkColorSet (object sender, System.EventArgs e)
		{
			MainClass.Settings.AltHyperLinkColour = clrAltHyperlink.Color;
		}

		protected virtual void OnTxtPasswordEditingDone (object sender, System.EventArgs e)
		{
			if(MainClass.Settings.StorePassword)
			{
				MainClass.Settings.Password = txtPassword.Text; //PLAIN TEXT!!! AHHH.
			}
		}

		protected virtual void OnRbActualActivated (object sender, System.EventArgs e)
		{
			MainClass.Settings.RelativeTime = rbRelative.Active;	
		}

		protected virtual void OnRbActualClicked (object sender, System.EventArgs e)
		{
			MainClass.Settings.RelativeTime = rbRelative.Active;	
		}	
	}
}
